<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TeamsController
 *
 * @author Zoila
 */
class TeamsController extends AppController{
    public $uses = array('Team','Project','ProjectTeam','User','Member','OrgMember');
    public $components=array('Search');
    //AUTORIZACION
    public function isAuthorized($user) {
        // All registered users can add posts
        if (in_array($this->action, array('view', 'add','nueva','addToProject'))) {
            return true;
        }

        // The owner of a post can edit and delete it
        if (in_array($this->action, array('edit', 'delete'))) {
            $stId = $this->request->params['pass'][0];
            if ($this->Team->isOwnedBy($stId, $user['id'])) {
                return true;
            }
        }

        return parent::isAuthorized($user);
    }
    
    public function view($id = null,$s =null) {
        
            $this->Team->id = $id;
            //$this->Team->unbindModelAll();
            $team=$this->Team->read();
            if ($this->Auth->user()!=NULL )
                $this->set('team',$team );

        if (($s != null) && ($s==='find')) {
            if ($this->request->is('post')){
                $arr=array();
                for($i=0;$i<count($team['Member']);$i++){
                    $arr[]=$team['Member'][$i]['user_id'];
                }

                $this->User->unbindModelAll();
                $users=$this->User->find('all', array('conditions' => array('NOT' => array('User.id' => $arr),'User.username LIKE'=>'%'.$this->request->data['Team']['buscar'].'%'),'order' => 'User.username'));
                $this->set('users',$users);

            }
        }

    }

    public function nueva( $idOrg = null){ 
        //ZP: desde la organizacion (NO ESTA LISTOO )
       if ($idOrg != null){
            if ($this->request->is('post')){
            
                
                $this->request->data['Team']['creator_id'] = $this->Auth->user('id');
                $this->request->data['Team']['organization_id'] = $idOrg;
                
                if($this->Team->save($this->request->data)){
                        //busco si existe el miembro en el equipo
                        
                        $this->Session->setFlash('Your team has been saved.');
                        $this->redirect(array('controller'=>'organizations','action'=>'view',$idOrg));
                        
                } 
                else{
                    $this->Session-> setFlash('Unable to add your team');
                }
            
            }
        }
        else {
                $this->set('error',true);
        }
        
    }
    public function addToProject($idPro=null){
        if ($this->request->data['ProjectTeam']['team_id']!=null) {
            $idteam=$this->request->data['ProjectTeam']['team_id'];
            $team=$this->Team->findById($idteam);
            if ($team!=null){
                $this->request->data['ProjectTeam']['name']=$team['Team']['name'];
            // $this->request->data['ProjectTeam']['detail']=$team['Team']['detail'];
                if ($this->ProjectTeam->save($this->request->data)){
                    $this->Session->setFlash('Your teamProject has been saved.');
                    $this->redirect(array('controller'=>'projects','action'=>'view',$idPro));
                }
                else{
                    $this->Session->setFlash('Unable to add your teamProject');
                }
            
            }
            else{
                    $this->Session->setFlash('Error al encontrar e equipo');
                }
        }
        else{
            $this->Session->setFlash('Es  necesario elegir un equipo ');
            $this->redirect(array('controller'=>'projects','action'=>'view',$idPro));
        }

    }
    public function add( $idPro = null){
        //ZP: desde el proyecto
        if ($idPro != null){
            if ($this->request->is('post')){
            
                $project=$this->Project->findById($idPro);
                $this->request->data['Team']['creator_id'] = $this->Auth->user('id');
                $this->request->data['Team']['organization_id'] = $project['Project']['organization_id'];
                
                if($this->Team->save($this->request->data)){
                    
                    //ZP: Guardar en la clase asociativa
                     $project_team = array('team_id'=> $this->Team->id,
                         'project_id'=> $idPro,
                         'creator_id'=> $this->Auth->user('id'),
                         'name' => $this->request->data['Team']['name'], 
                         'detail' =>  $this->request->data['Team']['detail']);
                  
                        if ($this->ProjectTeam->save($project_team)){
                            $this->Session->setFlash('Your team has been saved.');
                            $this->redirect(array('controller'=>'projects','action'=>'view',$idPro));
                        }
                        else{
                            $this->Session->setFlash('Unable to add your teamProject');
                        }
                    } 
                    else{
                        $this->Session-> setFlash('Unable to add your team');
                    }
            
            }
        }
        else {
                $this->set('error',true);
        }
    }

    


}

?>
